package com.ks.user.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ks.core.base.OperationContext;
import com.ks.core.constant.Const;
import com.ks.core.constant.DatePanel;
import com.ks.core.dto.SearchCriteriaBase;
import com.ks.core.exception.BizExceptionEnum;
import com.ks.core.exception.GunsException;
import com.ks.core.util.ToolUtil;
import com.ks.user.dao.ApTenantAllowMapper;
import com.ks.user.dao.PowerResurceMapper;
import com.ks.user.entity.ApBase;
import com.ks.user.dao.ApBaseMapper;
import com.ks.user.entity.ApTenantAllow;
import com.ks.user.entity.PowerResurce;
import com.ks.user.service.IApBaseService;
import com.ks.user.service._ServiceBase;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 应用基本信息表 服务实现类
 * </p>
 *
 * @author kingdee123
 * @since 2020-11-30
 */
@Service
public class ApBaseServiceImpl extends _ServiceBase<ApBaseMapper, ApBase> implements IApBaseService {

    @Resource
    private ApBaseMapper apBaseMapper;
    @Resource
    private ApTenantAllowMapper apTenantAllowMapper;
    @Resource
    private PowerResurceMapper powerResurceMapper;

    @Override
    public Map<String, Object> getApBaseAdminCount(OperationContext context) {
        Map<String, Object> map = new HashMap<>();
        EntityWrapper<ApBase> wrapper = new EntityWrapper<>();
        ApBase apBase = new ApBase();
        apBase.setDelMark(Const.DEL_FLAG_NORMAL);
        apBase.setSAppState(Const.DEL_FLAG_DEL);
        wrapper.setEntity(apBase);
        Integer integer = apBaseMapper.selectCount(wrapper);
        map.put("CardName", "应用发布数");
        map.put("DataCount", "1");
        List list = new ArrayList();
        DatePanel panel = new DatePanel();
        panel.setDataTime(new Date());
        panel.setDataValue(integer);
        panel.setValueType("STR");
        list.add(panel);
        map.put("date", list);
        return map;
    }

    @Override
    public Map<String, Object> getApBaseList(OperationContext context, SearchCriteriaBase searchCriteriaBase) {
        Map<String, Object> map = new HashMap<>();
        Page page = new Page(searchCriteriaBase.getPageIndex(), searchCriteriaBase.getLimit());
        EntityWrapper<ApBase> wrapper = new EntityWrapper<>();
        ApBase apBase = new ApBase();
        ArrayList<SearchCriteriaBase.matchField> fields = searchCriteriaBase.getFields();
        if (ToolUtil.isNotEmpty(fields)) {
            for (SearchCriteriaBase.matchField field : fields) {
                if ("iAppName".equals(field.getName())) {
                    apBase.setIAppName(field.getValue());
                }
                if ("iAppCode".equals(field.getName())) {
                    apBase.setIAppCode(field.getValue());
                }
            }
        }
        ArrayList<SearchCriteriaBase.StateField> states = searchCriteriaBase.getStates();
        if (ToolUtil.isNotEmpty(states)) {
            for (SearchCriteriaBase.StateField state : states) {
                if ("sAppState".equals(state.getName())) {
                    apBase.setSAppState(Integer.valueOf(state.getValue()));
                }
            }
        }
        if (context.getTenantId().intValue() == 0) {

        } else {
            apBase.setCAppType(Const.OrgBase.ORGANIZATION_TYPE);
        }
        wrapper.setEntity(apBase);
        Integer integer = apBaseMapper.selectCount(wrapper);
        map.put("count", integer);
        List<ApBase> apBases = apBaseMapper.selectPage(page, wrapper);
        map.put("apBases", apBases);
        return map;
    }

    @Override
    public Map<String, Object> getSysApBaseList(OperationContext context, SearchCriteriaBase searchCriteriaBase) {
        Map<String, Object> map = new HashMap<>();
        Page page = new Page(searchCriteriaBase.getPageIndex(), searchCriteriaBase.getLimit());
        EntityWrapper<ApBase> wrapper = new EntityWrapper<>();
        ApBase apBase = new ApBase();
        ArrayList<SearchCriteriaBase.matchField> fields = searchCriteriaBase.getFields();
        if (ToolUtil.isNotEmpty(fields)) {
            for (SearchCriteriaBase.matchField field : fields) {
                if ("iAppName".equals(field.getName())) {
                    apBase.setIAppName(field.getValue());
                }
                if ("iAppCode".equals(field.getName())) {
                    apBase.setIAppCode(field.getValue());
                }
            }
        }
        ArrayList<SearchCriteriaBase.StateField> states = searchCriteriaBase.getStates();
        if (ToolUtil.isNotEmpty(states)) {
            for (SearchCriteriaBase.StateField state : states) {
                if ("sAppState".equals(state.getName())) {
                    apBase.setSAppState(Integer.valueOf(state.getValue()));
                }
            }
        }
        apBase.setCAppType(Const.OrgBase.ORGANIZATION_TYPE);
        wrapper.setEntity(apBase);
        Integer integer = apBaseMapper.selectCount(wrapper);
        map.put("count", integer);
        List<ApBase> apBases = apBaseMapper.selectPage(page, wrapper);
        map.put("apBases", apBases);
        return map;
    }

    @Override
    public ApBase getApBaseInfo(int id) {
        ApBase apBase = apBaseMapper.selectById(id);
        return apBase;
    }

    @Override
    public int addApBase(OperationContext context, ApBase apBase) {
        Integer insert = apBaseMapper.insert(apBase);
        return insert;
    }

    @Override
    public int setAppOn(int appId, Integer appStatus) {
        ApBase apBase = apBaseMapper.selectById(appId);
        if (ToolUtil.isEmpty(apBase)) {
            throw new GunsException(BizExceptionEnum.APPLICATION_ERROR);
        }
        if (ToolUtil.isNotEmpty(appStatus)) {
            apBase.setSAppState(appStatus);
        }
        Integer integer = apBaseMapper.updateById(apBase);
        return integer;
    }

    @Override
    public int updateApBase(int appId, String appCode, String appName, String icon, String iServiceUrlRoot, String iPageUrl, String cAppScope, Integer cAppType) {
        ApBase apBase = apBaseMapper.selectById(appId);
        if (ToolUtil.isNotEmpty(apBase)) {
            apBase.setIAppCode(appCode);
            apBase.setIAppName(appName);
            apBase.setCAppScope(cAppScope);
            apBase.setIIcon(icon);
            apBase.setIServiceUrlRoot(iServiceUrlRoot);
            apBase.setIPageUrl(iPageUrl);
            apBase.setCAppType(cAppType);
        }
        Integer integer = apBaseMapper.updateById(apBase);
        return integer;
    }

    @Override
    public Map<String, Object> getTenantList(OperationContext context) {
        Map<String, Object> map = new HashMap<>();
        EntityWrapper<ApTenantAllow> wrapper = new EntityWrapper<>();
        ApTenantAllow apTenantAllow = new ApTenantAllow();
        List<ApBase> list = new ArrayList<>();
        if (context.getOrgId().intValue() == 0 && context.getOrgSubId().intValue() == 0) {
            //说明是租户管理员
            List<ApBase> sysApBase = getSysApBase();
            list.addAll(sysApBase);
            apTenantAllow.setCAllowType(Const.OrgBase.AUTHORIZATION_METHOD_L1);
            apTenantAllow.setTenantID(context.getTenantId().intValue());
            apTenantAllow.setDelMark(Const.DEL_FLAG_NORMAL);
            wrapper.setEntity(apTenantAllow);
            wrapper.orderBy("ID desc");
            Integer integer = apTenantAllowMapper.selectCount(wrapper);
            List<ApTenantAllow> apTenantAllows = apTenantAllowMapper.selectList(wrapper);
            if (ToolUtil.isNotEmpty(apTenantAllows)) {
                for (ApTenantAllow allow : apTenantAllows) {
                    ApBase apBase = apBaseMapper.selectById(allow.getApID());
                    if (apBase.getSAppState() == Const.DEL_FLAG_DEL) {
                        list.add(apBase);
                    }
                }
            }
            if (ToolUtil.isNotEmpty(list)) {
                for (ApBase apBase : list) {
                    List<PowerResurce> powerResurces = getList(apBase.getId());
                    apBase.setPower(powerResurces);
                }
            }
            map.put("list", list);
            map.put("count", integer + sysApBase.size());
        } else {
            //说明是租户下的员工
            List<ApBase> sysApBase = getSysApBase();
            for (ApBase apBase : sysApBase) {
                if (!apBase.getIAppCode().equals("JCGL") && !apBase.getIAppCode().equals("XCXGL")){
                    list.add(apBase);
                }
            }
            apTenantAllow.setCAllowType(Const.OrgBase.AUTHORIZATION_METHOD_L2);
            apTenantAllow.setTenantID(context.getTenantId().intValue());
            apTenantAllow.setDelMark(Const.DEL_FLAG_NORMAL);
            wrapper.setEntity(apTenantAllow);
            wrapper.orderBy("ID desc");
            Integer integer = apTenantAllowMapper.selectCount(wrapper);
            List<ApTenantAllow> apTenantAllows = apTenantAllowMapper.selectList(wrapper);
            if (ToolUtil.isNotEmpty(apTenantAllows)) {
                for (ApTenantAllow allow : apTenantAllows) {
                    ApBase apBase = apBaseMapper.selectById(allow.getApID());
                    if (apBase.getSAppState() == Const.DEL_FLAG_DEL) {
                        list.add(apBase);
                    }
                }
            }
            if (ToolUtil.isNotEmpty(list)) {
                for (ApBase apBase : list) {
                    List<PowerResurce> powerResurces = getList(apBase.getId());
                    apBase.setPower(powerResurces);
                }
            }
            map.put("list", list);
            map.put("count", integer + list.size());
        }
        return map;
    }

    public List<PowerResurce> getList(int apBaseId) {
        EntityWrapper<PowerResurce> wrapper = new EntityWrapper<>();
        PowerResurce powerResurce = new PowerResurce();
        powerResurce.setCApId(apBaseId);
        powerResurce.setCResourcesType(3);
        powerResurce.setDelMark(Const.DEL_FLAG_NORMAL);
        wrapper.setEntity(powerResurce);
        List<PowerResurce> powerList = powerResurceMapper.selectList(wrapper);
        return powerList;
    }


    public List<ApBase> getSysApBase() {
        EntityWrapper<ApBase> wrapper = new EntityWrapper<>();
        ApBase apBase = new ApBase();
        apBase.setDelMark(Const.DEL_FLAG_NORMAL);
        apBase.setCAppType(Const.DEL_FLAG_NORMAL);
        apBase.setSAppState(Const.DEL_FLAG_DEL);
        wrapper.setEntity(apBase);
        List<ApBase> list = apBaseMapper.selectList(wrapper);
        return list;
    }

    @Override
    public Map<String, Object> getSysTenantList(OperationContext context, SearchCriteriaBase searchCriteriaBase) {
        ArrayList<SearchCriteriaBase.matchField> fields = searchCriteriaBase.getFields();
        EntityWrapper<ApBase> wrapper = new EntityWrapper<>();
        ApBase apBase = new ApBase();
        apBase.setDataType("SYS");
        wrapper.setEntity(apBase);
        List<ApBase> apBases = apBaseMapper.selectList(wrapper);
        Map<String, Object> resultMap = this.isResultMap(apBases, searchCriteriaBase);
        return resultMap;
    }

    @Override
    public int isState(OperationContext context, ApBase apBase) {
        ApBase apBaseInfo = apBaseMapper.selectById(apBase.getId());
        if (ToolUtil.isNotEmpty(apBase)) {
            apBaseInfo.setSAppState(apBase.getSAppState());
        }
        Integer integer = apBaseMapper.updateById(apBaseInfo);
        return integer;
    }

    @Override
    public int updateSysApBase(OperationContext context, ApBase apBase) {
        Integer integer = apBaseMapper.updateById(apBase);
        return integer;
    }


    public Map<String, Object> isResultMap(List list, SearchCriteriaBase searchCriteriaBase) {
        Map<String, Object> map = new HashMap<>();
        map.put("count", list.size());
        map.put("limit", searchCriteriaBase.getLimit());
        map.put("pageIndex", searchCriteriaBase.getPageIndex());
        map.put("hasMore", false);
        map.put("items", list);
        return map;
    }
}
